let logger = require("pomelo-logger").getLogger(__filename);
let BaseTableService = require("../base/tableService");
let util = require('util');
let SoloTableCreator = require("./soloGameTable").create;
let PlayerCreator = require("../niuniu/niuniuPlayer").create;

let Service = function (app) {
    BaseTableService.call(this, app);
    this.gid = 10003;
};

util.inherits(Service, BaseTableService);

Service.prototype.getGid = function () {
    return this.gid;
};

Service.prototype.doCreatePlayer = function (playerData, frontendId, sessionId) {
    return PlayerCreator(playerData, frontendId, sessionId);
};

Service.prototype.doValidateCreateTableParams = function (params) {
    return {resultCode: 0, message: "ok"};
};

Service.prototype.doCreateYuanBaoTable  = function (tableCfg) {
    let table = SoloTableCreator(tableCfg);
    this.tables.push(table);
    return table;
};

Service.prototype.doPlayerReconnect = function(table, player) {
    player.push("onInitializeSoloGameTable", {
        table: table.toJSON(),
        myCards : player.getDisplayCards(table)
    });
};

Service.prototype.betForDealer = function (frontendId, sessionId, multiplier) {
    //0, 1, 2表示 不抢， 抢1倍，抢2倍
    if (multiplier !== 0 && multiplier !== 1 && multiplier !== 2) {
        return { resultCode: 404, message: "倍数不对"};
    }
    let table = this.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        return { resultCode: 404, message: "桌子不存在"};
    }
    let player = this.findPlayer(frontendId, sessionId);
    if (player == null) {
        return { resultCode: 404, message: "玩家不在线"};
    }
    if (!table.getIsPlaying()) {
        return { resultCode: 404, message: "游戏未开始"};
    }
    if (table.getStep() !== 1) {
        return { resultCode: 404, message: "桌子状态不对"};
    }
    if (player.getAction() !== 0) {
        return { resultCode: 404, message: "玩家已经操作了"};
    }
    table.betForDealer(player, multiplier);
    return { resultCode: 0, message: "ok"};
};

Service.prototype.bet = function (frontendId, sessionId, multiplier) {
    if (!multiplier) {
        return { resultCode: 404, message: "倍数为空"};
    }
    if (multiplier !== 1 && multiplier !== 2) {
        return { resultCode: 404, message: "倍数不对"};
    }
    let table = this.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        return { resultCode: 404, message: "桌子不存在"};
    }
    let player = this.findPlayer(frontendId, sessionId);
    if (player == null) {
        return { resultCode: 404, message: "玩家不在线"};
    }
    if (!table.getIsPlaying()) {
        return { resultCode: 404, message: "游戏未开始"};
    }
    if (table.getStep() !== 2) {
        return { resultCode: 404, message: "桌子状态不对"};
    }
    if (player.getAction() !== 0) {
        return { resultCode: 404, message: "玩家已经操作了"};
    }
    if (table.getDealerType() === 2 && table.getDealerUid() === player.getUid()) {
        return { resultCode: 404, message: "抢到庄的玩家不能再下注"};
    }
    table.bet(player, multiplier);
    return { resultCode: 0, message: "ok"};
};

Service.prototype.commit = function (frontendId, sessionId) {
    let table = this.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        return { resultCode: 404, message: "桌子不存在"};
    }
    let player = this.findPlayer(frontendId, sessionId);
    if (player == null) {
        return { resultCode: 404, message: "玩家不在线"};
    }
    if (!table.getIsPlaying()) {
        return { resultCode: 404, message: "游戏未开始"};
    }
    if (table.getStep() !== 3) {
        return { resultCode: 404, message: "桌子状态不对"};
    }
    if (player.getAction() !== 0) {
        return { resultCode: 404, message: "玩家已经操作了"};
    }
    table.commit(player);
    return { resultCode: 0, message: "ok"};
};

Service.prototype.giveUp = function (frontendId, sessionId, amount) {
    if (!amount) {
        return {resultCode: 404, message: "without amount"};
    }
    if (amount < 0) {
        return {resultCode: 404, message: "amount < 0"};
    }
    let sender = this.findPlayer(frontendId, sessionId);
    if (sender == null) {
        return { resultCode: 404, message: "玩家不在线"};
    }
    let table = this.findTableByPlayer(frontendId, sessionId);
    if (!table) {
        return { resultCode: 404, message: "桌子无效"};
    }
    let players = table.getAllPlayers();
    if (players.length !== 2) {
        logger.error('player num error');
        return {resultCode: 404, message: "人数不足"};
    }
    let receiver = null;
    if (players[0].getUid() === sender.getUid()) {
        receiver = players[1];
    } else if (players[1].getUid() === sender.getUid()){
        receiver = players[0];
    } else {
        logger.error('player not in this table');
        return {resultCode: 404, message: "所在房间错误"};
    }
    if (sender.getBalance() < amount) {
        logger.info('没有足够金币');
        return {resultCode: 404, message: "没有足够金币"};
    }
    let params = {uids: [sender.getUid(), receiver.getUid()], amounts:[-amount, amount], params:['giveup', 'giveup']};
    logger.info('solo give up success from %s to %s with acount: %d', sender.getNickname(), receiver.getNickname(), amount);
    return {
        resultCode: 0,
        message: "ok",
        params: params,
        table: table,
        receiverNickname: receiver.getNickname(),
        senderNickname: sender.getNickname()
    };
};

Service.prototype.getYuanBaoRoomByMultiplier = function (multiplier) {
    let rooms = this.app.get("soloRooms");
    for (let room of rooms) {
        if (room.multiplier === 2) {
            return room;
        }
    }
    return null;
};

Service.prototype.doCreateSystemTable = function (room) {
};

module.exports.create = function (app) {
    return new Service(app);
};